package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam1;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam01Fm extends Fragment implements View.OnClickListener {

    /**
     * 指示器参数 1组件
     */
    private static TextView hasElecVolt_1, hasElecVal_2, inputSmallElec_3,inputBigElec_4,onEarthModle_5,earthPlusCounts_6,
        shortFastCutFixVal_7,shortCutOverProFixVal_8,overElecProTimeMin_9,overElecProTimeMax_10;
    private Button param1ReadBtn;

    public Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param01,container,false);

        initView(view);
        initEvent();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        return view;
    }

    private void initEvent() {

        hasElecVolt_1.setOnClickListener(this);
        hasElecVal_2.setOnClickListener(this);
        inputSmallElec_3.setOnClickListener(this);
        inputBigElec_4.setOnClickListener(this);
        onEarthModle_5.setOnClickListener(this);
        earthPlusCounts_6.setOnClickListener(this);
        shortFastCutFixVal_7.setOnClickListener(this);
        shortCutOverProFixVal_8.setOnClickListener(this);
        overElecProTimeMin_9.setOnClickListener(this);
        overElecProTimeMax_10.setOnClickListener(this);

        param1ReadBtn.setOnClickListener(btnClickListenrer);
    }

    private void initView(View view){

        hasElecVolt_1 = (TextView) view.findViewById(R.id.hasElecVolt_1);
        hasElecVal_2 = (TextView) view.findViewById(R.id.hasElecVal_2);
        inputSmallElec_3 = (TextView) view.findViewById(R.id.inputSmallElec_3);
        inputBigElec_4 = (TextView) view.findViewById(R.id.inputBigElec_4);
        onEarthModle_5 = (TextView) view.findViewById(R.id.onEarthModle_5);
        earthPlusCounts_6 = (TextView) view.findViewById(R.id.earthPlusCounts_6);
        shortFastCutFixVal_7 = (TextView) view.findViewById(R.id.shortFastCutFixVal_7);
        shortCutOverProFixVal_8 = (TextView) view.findViewById(R.id.shortCutOverProFixVal_8);
        overElecProTimeMin_9 = (TextView) view.findViewById(R.id.overElecProTimeMin_9);
        overElecProTimeMax_10 = (TextView) view.findViewById(R.id.overElecProTimeMax_10);

        param1ReadBtn = (Button) view.findViewById(R.id.param1ReadBtn);
    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam1 obj =  DeviceParamInfoCache.getDeviceParamMap1().get(MainCahce.chooseDeviceAddr);
            if(obj != null){

                hasElecVolt_1.setText(obj.getHasElecVolt()+"");
                hasElecVal_2.setText(obj.getHasElecElec()+"");
                inputSmallElec_3.setText(obj.getInSmallElec()+"");
                inputBigElec_4.setText(obj.getInBigElec()+"");
                onEarthModle_5.setText(obj.getEarthIntervalTime()+"");
                earthPlusCounts_6.setText(obj.getEarthPulsCounts()+"");
                shortFastCutFixVal_7.setText(obj.getShortSudValue()+"");
                shortCutOverProFixVal_8.setText(obj.getShortOverProtectValue()+"");
                overElecProTimeMin_9.setText(obj.getOverProtectMinValue()+"");
                overElecProTimeMax_10.setText(obj.getOverProtectMaxValue()+"");
            }else {
                hasElecVolt_1.setText(" ");
                hasElecVal_2.setText(" ");
                inputSmallElec_3.setText(" ");
                inputBigElec_4.setText(" ");
                onEarthModle_5.setText(" ");
                earthPlusCounts_6.setText(" ");
                shortFastCutFixVal_7.setText(" ");
                shortCutOverProFixVal_8.setText(" ");
                overElecProTimeMin_9.setText( " ");
                overElecProTimeMax_10.setText( " ");
            }
        }
    };
    public static void freshDeviceParam1Model() {
        handler1.sendMessage(new Message());
    }

    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param1ReadBtn:{
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 1);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };

    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.hasElecVolt_1:{
                index = 1;
                str = hasElecVolt_1.getText().toString();
                break;
            }
            case R.id.hasElecVal_2:{
                index = 2;
                str = hasElecVal_2.getText().toString();
                break;
            }
            case R.id.inputSmallElec_3:{
                index = 3;
                str = inputSmallElec_3.getText().toString();
                break;
            }
            case R.id.inputBigElec_4:{
                index = 4;
                str = inputBigElec_4.getText().toString();
                break;
            }case R.id.onEarthModle_5:{
                index = 5;
                str = onEarthModle_5.getText().toString();
                break;
            }case R.id.earthPlusCounts_6:{
                index = 6;
                str = earthPlusCounts_6.getText().toString();
                break;
            }case R.id.shortFastCutFixVal_7:{
                index = 7;
                str = shortFastCutFixVal_7.getText().toString();
                break;
            }case R.id.shortCutOverProFixVal_8:{
                index = 8;
                str = shortCutOverProFixVal_8.getText().toString();
                break;
            }
            case R.id.overElecProTimeMin_9:{
                index = 9;
                str = overElecProTimeMin_9.getText().toString();
                break;
            }
            case R.id.overElecProTimeMax_10:{
                index = 10;
                str = overElecProTimeMax_10.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                            IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,1,index,Integer.valueOf(edit.getText().toString()));
                            MinaClientThread.session.write(buffer);
                        }catch (Exception e){
                            Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                        }
                    }
                }) .setNegativeButton("取消", null).show();
    }

}
